import { createRouter, createWebHistory } from "vue-router";
import login from "../views/login.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "login",
      component: login,
    },
    {
      path: "/homeview",
      name: "homeview",
      component: () => import("../views/HomeView.vue"),
      children: [
        {
          path: "/home",
          name: "home",
          component: () => import("../views/home/home.vue"),
          meta: {
            requireAuth: true,
            title: "面板",
          },
        },
        {
          path: "/permission",
          name: "permission",
          meta: {
            requireAuth: true,
            title: "权限管理",
          },
          children: [
            {
              path: "/permission/user",
              name: "user",
              component: () => import("../views/permission/user.vue"),
              meta: {
                requireAuth: true,
                title: "用户管理",
              },
            },
            {
              path: "/permission/role",
              name: "role",
              component: () => import("../views/permission/role.vue"),
              meta: {
                requireAuth: true,
                title: "角色管理",
              },
            },
            {
              path: "/permission/menu",
              name: "menu",
              component: () => import("../views/permission/menu.vue"),
              meta: {
                requireAuth: true,
                title: "菜单管理",
              },
            },
          ],
        },
        {
          path: "/train",
          name: "train",
          meta: {
            requireAuth: true,
            title: "培训管理",
          },
          children: [
            {
              path: "/train/classroom",
              name: "/classroom",
              component: () => import("../views/train/classroom.vue"),
              meta: {
                requireAuth: true,
                title: "课堂培训",
              },
            },
            {
              path: "/train/afterclass",
              name: "/afterclass",
              component: () => import("../views/train/afterclass.vue"),
              meta: {
                requireAuth: true,
                title: "课后训练",
              },
            },
            {
              path: "/train/character",
              name: "/character",
              component: () => import("../views/train/character.vue"),
              meta: {
                requireAuth: true,
                title: "品德教育",
              },
            },
          ],
        },
      ],
    },
  ],
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token") || to.path === "/") {
    next();
  } else {
    next("/");
  }
});

export default router;
